<?php

use app\assets\ApiAsset;
use app\assets\TableAsset;
use app\widgets\ManagerTableOp;
use yii\helpers\Html;
use yii\helpers\Url;

/**
 * @var $this yii\web\View
 * @var $publishList app\models\AgentPublish[]
 */

ApiAsset::register($this);
TableAsset::register($this);

$this->title = '信息列表';
$this->params['breadcrumbs'][] = '宣传管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<?php echo Html::beginForm('?', 'get', ['class'=>'form-inline']);?>
<div class="form-group">
    <a href="<?php echo Url::to(['/agent/publish/edit']);?>" class="btn btn-success btn-sm">添加</a>
</div>
<?php echo Html::endForm();?>
<table class="table table-striped table-bordered table-hover">
    <thead>
    <tr>
        <th class="center">
            <label class="pos-rel">
                <input type="checkbox" class="ace" />
                <span class="lbl"></span>
            </label>
        </th>
        <th>名称</th>
        <th>内容</th>
        <th>排序</th>
        <th>操作</th>
    </tr>
    </thead>

    <tbody>
    <?php foreach ($publishList as $publish) {?>
        <tr id="data_<?php echo $publish->id;?>">
            <td class="center"><label class="pos-rel"><input type="checkbox" class="ace" value="<?php echo $publish->id;?>" /><span class="lbl"><?php echo $publish->id;?></span></label></td>
            <td><?php echo Html::encode($publish->name);?></td>
            <td><?php echo Html::encode($publish->value);?></td>
            <td><?php echo $publish->sort;?></td>
            <td><?php echo ManagerTableOp::widget(['items' => [
                    ['icon' => 'fa fa-pencil', 'href' => Url::to(['/agent/publish/edit', 'id' => $publish->id]), 'btn_class' => 'btn btn-xs btn-success', 'tip' => '修改', 'color' => 'green'],
                    ['icon' => 'fa fa-long-arrow-up', 'onclick' => 'moveUp(' . $publish->id . ')', 'btn_class' => 'btn btn-xs', 'tip' => '上移'],
                    ['icon' => 'fa fa-long-arrow-down', 'onclick' => 'moveDown(' . $publish->id . ')', 'btn_class' => 'btn btn-xs', 'tip' => '下移'],
                    // ['icon' => 'fa fa-trash', 'onclick' => 'deletePublish(' . $publish->id . ')', 'btn_class' => 'btn btn-xs btn-danger', 'tip' => '删除', 'color' => 'red'],
                ]]);?></td>
        </tr>
    <?php }?>
    </tbody>
</table>
<script>
    /**
     * 删除信息
     * @param id integer 编号
     */
    function deletePublish(id) {
        if (!confirm('确定要删除吗？')) {
            return false;
        }
        $.getJSON('<?php echo Url::to(['/agent/publish/delete']);?>', {id: id}, function (json) {
            if (callback(json)) {
                $('#data_' + id).remove();
            }
        });
    }

    /**
     * 上移
     * @param id
     */
    function moveUp(id) {
        $.getJSON('<?php echo Url::to(['/agent/publish/sort']);?>', {id: id, sort: 1}, function (json) {
            if (callback(json)) {
                window.location.reload();
            }
        });
    }

    /**
     * 下移
     * @param id
     */
    function moveDown(id) {
        $.getJSON('<?php echo Url::to(['/agent/publish/sort']);?>', {id: id, sort: -1}, function (json) {
            if (callback(json)) {
                window.location.reload();
            }
        });
    }
</script>
